package com.lxyk.controller.statistic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.lxyk.dao.user.ClassesDao;
import com.lxyk.domain.*;
import com.lxyk.service.statistic.ClassAttendanceService;
import com.lxyk.service.statistic.StuAttendanceService;
import com.lxyk.service.statistic.StuTaskService;
import com.lxyk.service.user.ClassesService;
import com.lxyk.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.ldap.PagedResultsControl;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

//班级数据统计
@Controller
@RequestMapping("/statistic")
public class ClassAttendanceController {
    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassAttendanceService classAttendanceService;
    @Autowired
    private UserService userService;

    //进入统计管理页:查询所有班级
    @RequestMapping("/toStatistic")
    public String  toStatistic(HttpServletRequest request){
//        PageBean<Classes> all = classesService.findAll(1, 100);
//        System.out.println(all.getList());
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //根据用户id查询对应班级
        List<Classes> list = classesService.findByUser(user.getId());
        request.setAttribute("list",list);
        return "data-class";
    }

    //班级每个月统计数据:每个月平均到课率/作业率
    @RequestMapping("/classMonthAvgDate")
    public String classMonthAvgDate(Integer cid, HttpServletRequest request){
        //每个月平均到课率
        System.out.println(cid);
        List<ClassStatisticBO> data = classAttendanceService.findData(cid);
        System.out.println(data);
        String dataJson = JSONArray.toJSONString(data,true);
        System.out.println(dataJson);
        request.setAttribute("cid",cid);
        request.setAttribute("statistic",dataJson);
        return "data-statistic";
    }

    //班级当月每天的统计数据
    @RequestMapping("/classDayData")
    public String classDayAttendanceData(Integer cid,Date date,HttpServletRequest request){
        List<ClassAttendance> dayData = classAttendanceService.findDayDate(cid, date);
        System.out.println(dayData);
        String jsonStr = JSONArray.toJSONString(dayData, true);
        System.out.println(jsonStr);
        request.setAttribute("cid",cid);
        request.setAttribute("dayData",jsonStr);
        return "data-detail";
    }




}
